/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-5-7
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.sysmgr.org;

import java.util.List;


import com.pearlsoft.util.StringUtils;
import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dao.RecrodRowMapper;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;
import com.pearlsoft.webapp.sysmgr.org.service.OrgRowMapperHelper;
import com.pearlsoft.webapp.sysmgr.org.service.OrgService;

/**
 *描述： 
 *
 * 
 */
@QueryHandlerComponent(name = "OrgQueryHandler")
public class OrgQueryHandler extends AbsBaseQueryHandler{
	private final String generalFields=" {0}.ORG_ID,{0}.ORG_SIMPLE_NAME, {0}.ORG_FULL_NAME, {0}.ORG_PARENT_ID,{0}.ORG_ORDER,{0}.C_STATUS,{0}.C_REMARK";
	private final RecrodRowMapper generalRowMapper=OrgRowMapperHelper.generalRowMapper();

	public void handler(Query query, DataSet ds) throws Exception {
		
	}


	@Override
	public void getEntity(Query query, DataSet ds) throws Exception {
		String objId =(String) query.get("objId");
		OrgService orgService = this.getBean("orgService",OrgService.class);
		Record record = orgService.getById(objId);
		ds.addRecrod(record);
	}
	
	public void getChildren(Query query, DataSet ds) throws Exception {
		String parentId = (String) query.get("node");
		OrgService orgService = this.getBean("orgService",OrgService.class);
		List<Record> list = orgService.getChildren(parentId);
		ds.setRecords(list);
	}

	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		String searchValue = (String)query.get("searchValue") ;
		OrgService orgService = this.getBean("orgService",OrgService.class);
		StringBuilder sqlbuffer = new StringBuilder();
		String sql="select "+StringUtils.format(generalFields, "t")+" from SYS_ORG t ";
		sqlbuffer.append(sql);
		if( !StringUtils.isBlank(searchValue) ){
			sqlbuffer.append(" where ");
			appendLike(sqlbuffer, "ORG_SIMPLE_NAME", searchValue);
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "ORG_FULL_NAME", searchValue);
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "C_STATUS", searchValue);	
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "C_REMARK", searchValue);			
		}
		sqlbuffer.append(" ORDER BY ORG_PARENT_ID,ORG_ORDER ");
		List<Record> list  = orgService.getBaseSimpleJdbcDao().query(sqlbuffer.toString(), generalRowMapper);
		ds.setRecords(list);
	}


	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		OrgService orgService = this.getBean("orgService",OrgService.class);
		StringBuilder sqlbuffer = new StringBuilder();
		String sql="select "+StringUtils.format(generalFields, "t")+" from SYS_ORG t ";
		
		String ORG_SIMPLE_NAME =(String)query.get("ORG_SIMPLE_NAME") ;
		String ORG_FULL_NAME =(String)query.get("ORG_FULL_NAME") ;
		String C_STATUS =(String)query.get("C_STATUS") ;
		String C_REMARK =(String)query.get("C_REMARK") ;
		sqlbuffer.append(sql);
		boolean hasWhere = false;
		hasWhere=appendAndWhereLike(sqlbuffer, hasWhere, "ORG_SIMPLE_NAME", ORG_SIMPLE_NAME);
		hasWhere=appendAndWhereLike(sqlbuffer, hasWhere, "ORG_FULL_NAME", ORG_FULL_NAME);
		hasWhere=appendAndWhereLike(sqlbuffer, hasWhere, "C_STATUS", C_STATUS);
		hasWhere=appendAndWhereLike(sqlbuffer, hasWhere, "C_REMARK", C_REMARK);
		sqlbuffer.append(" ORDER BY ORG_PARENT_ID,ORG_ORDER ");
		List<Record> list  = orgService.getBaseSimpleJdbcDao().query(sqlbuffer.toString(), generalRowMapper);
		ds.setRecords(list);
	}

}
